package cn.swing.his.finance.webapi.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.swing.his.common.web.JsonPage;
import cn.swing.his.common.web.JsonResult;
import cn.swing.his.finance.service.ISalaryService;
import cn.swing.his.finance.webapi.service.impl.SalaryServiceImpl;
import cn.swing.his.pojo.finance.dto.AddSalaryDTO;
import cn.swing.his.pojo.finance.dto.TimeDTO;
import cn.swing.his.pojo.finance.entity.FinanceDetail;
import cn.swing.his.pojo.finance.vo.SalaryVO;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/finance/salary")
@Api(tags = "工资管理")
public class SalaryController {
    @Autowired
    private ISalaryService salaryService;

    @PostMapping("/list")
    @ApiOperation("预发放工资医生表")
    public JsonResult<JsonPage<SalaryVO>> salaryList(Integer pageNum,Integer pageSize,Integer index){
        System.out.println(index);
        JsonPage<SalaryVO> jsonPage=salaryService.salaryList(pageNum, pageSize,index);
        return JsonResult.ok("成功",jsonPage);
    }

    @GetMapping("/payoff")
    @ApiOperation("发放工资，修改状态")
    @ApiImplicitParam(value = "id",name = "id",example = "1")
    public JsonResult<Void> updateSalaryState(Integer id){
        salaryService.updateSalaryState(id);
        return JsonResult.ok();
    }

    @GetMapping("/batch")
    @ApiOperation("批量处理工资，修改状态")
    public JsonResult<Void> updateBatchSalaryState(Integer[] ids){
        for (Integer id:ids) {
            salaryService.updateBatchSalaryState(id);
        }
        return JsonResult.ok();
    }
    /**
     * 根据时间导出
     * @param response
     * @param startDate
     * @param endDate
     * @throws Exception
     */
    @ApiOperation(value = "根据时间导出申请表")
    @GetMapping("/export")
    public void exportDetail(HttpServletResponse response, String startDate, String endDate) throws Exception {
        TimeDTO time = new TimeDTO();
        time.setStartDate(startDate);
        time.setEndDate(endDate);
        List<SalaryVO> list = salaryService.salaryAllList(time);
        ExcelWriter writer = ExcelUtil.getWriter(true);
//        ExcelWriter writer = ExcelUtil.getWriter(true);
        log.info("list:{}",list);
        log.info("成功");
        //自定义标题别名
        writer.addHeaderAlias("id","id");
        writer.addHeaderAlias("name", "名字");
        writer.addHeaderAlias("salaryDetails", "工资");
        writer.addHeaderAlias("state", "状态");
        writer.addHeaderAlias("deliveryTime", "发放时间");
        writer.addHeaderAlias("description", "工资备注");
        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(list, true);
        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("工资发放情况信息(时间)", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();
        log.info("成功！");
    }

    @PostMapping("/insert")
    @ApiOperation("插入医生工资")
    public JsonResult<Void> insertSalary(AddSalaryDTO addSalaryDTO){
        salaryService.insertSalary(addSalaryDTO);
        return JsonResult.ok();
    }
}
